using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class JobSeekers_savedsearchs : System.Web.UI.Page
{
    string connectionString = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString =
                     WebConfigurationManager.ConnectionStrings[
                       "RecruitVietnam"].ConnectionString;
        if (Session.Contents["jsuserid"] == null)
        {
            Response.Redirect("login.aspx");
        }
        if (!IsPostBack)
            ShowData();

    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        ShowData();
        
    }
    void ShowData()
    {
        try
        {
            SqlServerProvider cls = new SqlServerProvider(connectionString);
            cls.CommandText = "SPViewSavedSearch";
            cls.ParameterCollection = new string[1] { "@UserID"};
            cls.ValueCollection = new string[1] { 
            Convert.ToString(Session.Contents["jsuserid"]) };
            cls.CommandType = CommandType.StoredProcedure;
            DataTable dataTable = cls.GetDataTable();
            GridViewHelper gridViewHelper = new GridViewHelper();
            gridViewHelper.FillDataTableToGridView(
            dataTable, this.GridView1);
            
            if (GridView1.Rows.Count >= 1)
                btnDelete.Enabled = true;
            else
                btnDelete.Enabled = false;
        }
        catch (Exception ex)
        {
            Literal1.Text = "ShowData:" + ex.Message;
        }
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    { 
        Server.Transfer("jobresult.aspx?indexRow=" + e.RowIndex);
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
    try
    {
        string searchID = "";
        foreach (GridViewRow gridViewRow in GridView1.Rows)
        {
            CheckBox checkBox = (CheckBox)
            gridViewRow.FindControl("chkSearchID");
            if (checkBox.Checked)
            {
                try
                {
                    searchID +=
                    gridViewRow.Cells[1].Text + ",";
                }
                catch (Exception ex)
                {
                    Literal1.Text = ex.Message;
                }
            }

        }
        if (searchID != "")
        {
            searchID = searchID.Substring(0, searchID.Length - 1);
            RecruitVietnam recruitVietnam
                        = new RecruitVietnam();
            recruitVietnam.ConnectionString = connectionString;
            foreach (string search in searchID.Split(','))
                recruitVietnam.DeleteSavedSearch(
                Convert.ToString(Session["jsuserid"]), search);
            ShowData();
        }
    }
    catch (Exception ex)
    {
        Literal1.Text = "Delete:" + ex.Message;
    }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow gridViewRow = e.Row;
        gridViewRow.Cells[6].Visible = false;
        gridViewRow.Cells[4].Visible = false;
    }
}
